<template>
  <view class="uni-swiper-page orderListWrap">
    <view style="flex: 1; background-color: #f2f2f2; width: 100%; height: 100%">
      <view style="background-color: #fff;padding: 24rpx 30rpx">
        <up-search placeholder="请输入收件人昵称" :show-action="false" height="68" v-model="keyword"
        searchIconSize="34" inputStyle="font-size: 24rpx" @search="search"></up-search>
      </view>
      <view style="height: calc(100% - 116rpx);width: 100%;">
        <common-scroll-view :loading="loading" :pageNum="pageNum" :pageSize="pageSize" :total="total" :data="dataList"
          @onRefresh="onRefreshBack" @getMore="getMore" :noCreate="true">
          <OrderItem  v-for="item in dataList" :key="item.tradeNo" :item="item" :type="type" @refreshList="finish"></OrderItem>
        </common-scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
import dayjs from 'dayjs';
import OrderItem from './OrderItem.vue';
import { getOrderList } from '@/services/order';

export default {
  components: {
    OrderItem,
  },
  props: {
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      keyword: '',
      dataList: [],
      total: 0,
      pageSize: 10,
      pageNum: 1,
      loading: false,
      canRefresh: false,
      buttonStyle: {
        width: '132rpx',
        margin: '0 0 0 12rpx',
        minWidth: '132rpx'
      }
    }
  },
  methods: {
    isPayOrder(type) {
      return Number(type) === 0;
    },
    search() {
      this.getListAction(1);
    },
    async getListAction(num) {
      if (this.loading) return;
      this.loading = true;
      const params = {
        limit: this.pageSize,
        page: num,
      }
      switch(this.type){
        case '1':
          params.isComplete = '1';
          params.isPack = '1';
          params.isReturn = '1';
          break;
        case '2':
          params.isComplete = '2';
          params.isPack = '2';
          params.isReturn = '1';
          break;
        case '3':
          params.isComplete = '1';
          params.isPack = '2';
          params.isReturn = '1';
          break;
        case '4':
          params.isComplete = '1';
          params.isPack = '1';
          params.isReturn = '2';
          break;
        default:
          break;
      }
      if (this.keyword) {
        params.nickname = this.keyword;
      }
      const res = await getOrderList(params)
      if (res.success) {
        this.pageNum = num;
        const { data = [], total = 0 } = res?.data || {};
        this.total = total || 0;
        let list = [...(data || [])];
        if (num === 1) {
          this.dataList = list;
        } else {
          this.dataList = this.dataList.concat(list);
        }
      }
      this.loading = false;
    },
    async onRefreshBack(callback) {
      console.log('onRefreshBack', this.canRefresh);
      if (this.canRefresh) {
        await this.getListAction(1);
      }
      callback();
    },
    getMore() {
      this.getListAction(this.pageNum + 1);
    },
    async loadData() {
      this.canRefresh = true;
      await this.getListAction(1);
    },
    clear() {
      // 释放数据时被调用，参考 swiper-list 缓存配置
      this.dataList.length = 0;
    },
    goActivityRule() {
      uni.navigateTo({
        url: '/pages/activityRule/index'
      });
    },
    refund(item) {
      const _that = this;
      // pages/refund/index
      uni.navigateTo({
        url: `/pages/refund/index`,
        success: (res) => {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('orderDetail', {
            data: item,
          })
        }
      });
      // uni.showModal({
      //   title: '是否确认退款',
      //   confirmText: '确认退款',
      //   cancelText: '再想想',
      //   success: async (res) => {
      //     if (res.confirm) {
      //       _that.$emit('switchTab', 4)
      //       // const v = await deleteAddress(id);
      //       // if (v.code === 200) {
      //       //   const eventChannel = _that.getOpenerEventChannel();
      //       //   eventChannel?.emit('deleteAddress', id);
      //       //   _that.timer = setTimeout(() => {
      //       //     uni.navigateBack();
      //       //   }, 1500)
      //       //   uni.$u.toast('删除成功！')
      //       // } else {
      //       //   uni.$u.toast('删除失败！')
      //       // }
      //     }
      //   }
      // })
    },
    expressTracking(item) {
      uni.navigateTo({
        url: `/pages/expressTracking/index?id=${item.id}`,
      });
    },
    evaluate(item) {
      const _that = this;
      uni.navigateTo({
        url: `/pages/evaluate/index`,
        success: (res) => {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('orderDetail', {
            data: item,
          })
        }
      });
    },
    getRemainingTime(time) {
      const endTime = dayjs(time).add(1, 'day').valueOf() - dayjs().valueOf();
      if (endTime > 0) {
        return endTime;
      }
      return 0;
    },
    finish() {
      console.log('refreshList');
      this.$emit('refreshList')
    },
  }
}
</script>

<style lang="scss">
  .orderListWrap {
    button::after {
      content: '';
      display: none;
    }
  }
</style>

<style lang="scss" scoped>
.uni-swiper-page {
  display: flex;
  flex: 1;
  height: 100%;
  width: 100%;
  flex-direction: column;
  align-items: center;
}
.itemWrap{
  margin: 0 30rpx 60rpx;
  width: calc(100% - 120rpx);
  padding: 30rpx;
  background-color: #fff;
  border-radius: 16rpx;
}
</style>